package com.shuqi.activity;

import java.util.HashMap;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.shuqi.activity.viewport.BookContentView;
import com.shuqi.activity.viewport.CommonTitle;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.controller.R;
import com.shuqi.controller.R.id;
import com.shuqi.statistics.StatisticsEventID;
import com.shuqi.statistics.StatisticsUtils;

/**
 * 阅读页菜单更多设置
 * 
 * @author Jianda.Yang
 * 
 * @Date 2014-1-7 下午3:21:29
 */
public class BookContentMoreSetActivity extends ActivityBase implements OnClickListener,
        OnCheckedChangeListener {

    private static final int NEVER_SLEEP_TIME = 36000000;

    private static final int SYSTEM_TIME = -2;

    /* 屏保时间 */
    private TextView two, five, ten, foreverLight;
    /* 翻页动画 */
    private TextView mTurnModeReal, mMoveLeftRight, mScrollUpdown;

    ToggleButton btnSoundToggle = null;
    ToggleButton btnSideToggle = null;
    ToggleButton btnFullScreenToggle = null;

    /** 是否修改了点击两侧翻页按钮状态 */
    private boolean isClicKSide;
    /** 是否修改了音量键状态 */
    private boolean isClickSound;
    /** 是否修改了全屏模式 */
    private boolean isClickFullScreen;
    private CommonTitle mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_bookcontentmoreset);
        findByID();
        init();
        initScreenTime();
        initTurnMode();
        setClickListener();

    }

    private void init() {
        mTitle = (CommonTitle) findViewById(R.id.title);
        mTitle.setLeftClickListener(this);
        five.setTextColor(getResources().getColor(R.color.common_white));
        five.setBackgroundResource(R.drawable.btn_sel);
        // simulation.setTextColor(getResources().getColor(R.color.common_white));
        // simulation.setBackgroundResource(R.drawable.btn_sel);

        // 两侧翻页
        this.isClicKSide =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentTurnPageMode();
        btnSideToggle.setChecked(this.isClicKSide);
        btnSideToggle.setOnCheckedChangeListener(this);// 添加监听事件

        // 音量键翻页
        this.isClickSound =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                        .getBookContentVolumeEnabled();
        btnSoundToggle.setChecked(this.isClickSound);
        btnSoundToggle.setOnCheckedChangeListener(this);// 添加监听事件

        // 全屏模式
        isClickFullScreen =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentFullScreen();
        btnFullScreenToggle.setChecked(!isClickFullScreen);
        btnFullScreenToggle.setOnCheckedChangeListener(this);// 添加监听事件
    }

    private void findByID() {
        btnSoundToggle = (ToggleButton) findViewById(R.id.volume_turnpage_on_or_off);
        btnSideToggle = (ToggleButton) findViewById(R.id.click_side_turnpage_on_or_off);
        btnFullScreenToggle = (ToggleButton) findViewById(R.id.full_screen_on_or_off);

        // findViewById(R.id.bookcontentmoreset_preview_rel).setOnClickListener(this);
        two = (TextView) findViewById(R.id.bookcontentmoreset_two_minute_tv);
        five = (TextView) findViewById(R.id.bookcontentmoreset_five_minute_tv);
        ten = (TextView) findViewById(R.id.bookcontentmoreset_ten_minute_tv);
        foreverLight = (TextView) findViewById(R.id.bookcontentmoreset_foreverLight_tv);

        mTurnModeReal = (TextView) findViewById(R.id.turn_mode_real);
        mMoveLeftRight = (TextView) findViewById(R.id.turn_mode_move_leftright);
        mScrollUpdown = (TextView) findViewById(R.id.turn_mode_scroll_updown);
    }

    private void setClickListener() {
        two.setOnClickListener(this);
        five.setOnClickListener(this);
        ten.setOnClickListener(this);
        foreverLight.setOnClickListener(this);
        // simulation.setOnClickListener(this);
        // leftORrightMove.setOnClickListener(this);

        mTurnModeReal.setOnClickListener(this);
        mMoveLeftRight.setOnClickListener(this);
        mScrollUpdown.setOnClickListener(this);
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.click_side_turnpage_on_or_off:
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentTurnpagemode(
                        btnSideToggle.isChecked());
                StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                        StatisticsEventID.BOOKCONTENT_EITHERSIDE_PAGE);
                break;
            case R.id.volume_turnpage_on_or_off:
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                        .saveBookContentVolumeEnabled(btnSoundToggle.isChecked());
                StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                        StatisticsEventID.BOOKCONTENT_VOLUME);
                break;
            case R.id.full_screen_on_or_off:
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentFullScreen(
                        !btnFullScreenToggle.isChecked());
                if(btnFullScreenToggle.isChecked()){//非全屏
                    StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                            StatisticsEventID.BOOKCONTENT_FULL_SCREEN_NO);
                }else{
                    StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                            StatisticsEventID.BOOKCONTENT_FULL_SCREEN);
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left: {
                finish();
            }
                break;
            case R.id.bookcontentmoreset_two_minute_tv:
                showSelect(0);
                setScreenTime(5 * 60 * 1000);
                /* 屏保时间，仅用于统计 */
                HashMap<String, String> screenTimeSave = new HashMap<String, String>();
                screenTimeSave.put("screenTime", "5");
                StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                        StatisticsEventID.BOOKCONTENT_SCREEN_SAVETIMEOUT_FIVE);
                if (screenTimeSave != null && screenTimeSave.size() > 0) {
                    screenTimeSave.clear();
                    screenTimeSave = null;
                }
                break;
            case R.id.bookcontentmoreset_five_minute_tv:
                showSelect(1);
                setScreenTime(10 * 60 * 1000);
                HashMap<String, String> screenTimeSave5 = new HashMap<String, String>();
                screenTimeSave5.put("screenTime", "10");
                StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                        StatisticsEventID.BOOKCONTENT_SCREEN_SAVETIMEOUT_TEN);
                if (screenTimeSave5 != null && screenTimeSave5.size() > 0) {
                    screenTimeSave5.clear();
                    screenTimeSave5 = null;
                }
                break;
            case R.id.bookcontentmoreset_ten_minute_tv:
                showSelect(2);
                setScreenTime(SYSTEM_TIME);
                HashMap<String, String> screenTimeSave10 = new HashMap<String, String>();
                screenTimeSave10.put("screenTime", "10");// TODO 系统时间统计
                StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                        StatisticsEventID.BOOKCONTENT_SCREEN_SAVETIMEOUT_SYSTEM);
                if (screenTimeSave10 != null && screenTimeSave10.size() > 0) {
                    screenTimeSave10.clear();
                    screenTimeSave10 = null;
                }
                break;
            case R.id.bookcontentmoreset_foreverLight_tv:
                showSelect(3);
                setScreenTime(NEVER_SLEEP_TIME);
                HashMap<String, String> screenTimeSaveNever = new HashMap<String, String>();
                screenTimeSaveNever.put("screenTime", "-1");// 由于统计不能传汉字，-1代表屏幕常亮
                StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                        StatisticsEventID.BOOKCONTENT_SCREEN_SAVETIMEOUT_ALWAYS);
                if (screenTimeSaveNever != null && screenTimeSaveNever.size() > 0) {
                    screenTimeSaveNever.clear();
                    screenTimeSaveNever = null;
                }
                break;
            case R.id.bookcontentmoreset_preview_rel: {
                // Intent intent = new Intent();
                // intent.setClass(BookContentMoreSetActivity.this,
                // BookContentMoreSetPreviewActivity.class);
                // intent.putExtra("previewLineSpace", getIntent().getIntExtra("previewLineSpace",
                // BookContentSettings.MIN_LINESPACE));
                // intent.putExtra("previewContent", getIntent().getStringExtra("previewContent"));
                // startActivity(intent);
                // StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                // StatisticsEventID.BOOKCONTENT_VIEW_LAYOUT);
            }
                break;

            case R.id.turn_mode_real:
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentPageTurnMode(
                        BookContentView.PAGETURN_MODE_BOOK);
                showTurnModeSelect(BookContentView.PAGETURN_MODE_BOOK);
                StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                        StatisticsEventID.BOOKCONTENT_REAL);
                break;
            case R.id.turn_mode_move_leftright:
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentPageTurnMode(
                        BookContentView.PAGETURN_MODE_SMOOTH);
                showTurnModeSelect(BookContentView.PAGETURN_MODE_SMOOTH);
                StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                        StatisticsEventID.BOOKCONTENT_LEFT_RIGHT);
                break;
            case R.id.turn_mode_scroll_updown:
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentPageTurnMode(
                        BookContentView.PAGETURN_MODE_SCROLL);
                showTurnModeSelect(BookContentView.PAGETURN_MODE_SCROLL);
                StatisticsUtils.onEvent(BookContentMoreSetActivity.this,
                        StatisticsEventID.BOOKCONTENT_UPDOWN);
                break;
        }
    }

    /**
     * 显示选择的屏保时间或翻页动画
     * 
     * @remark 0:2分钟；1:5分钟；2:10分钟；3:常亮；4:仿真翻页；5:左右平移
     * @param index
     */
    private void showSelect(int index) {
        switch (index) {
            case 0:
                two.setTextColor(getResources().getColor(R.color.common_white));
                five.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                ten.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                foreverLight
                        .setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                two.setBackgroundResource(R.drawable.btn_sel);
                five.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                ten.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                foreverLight.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                break;
            case 1:
                two.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                five.setTextColor(getResources().getColor(R.color.common_white));
                ten.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                foreverLight
                        .setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                two.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                five.setBackgroundResource(R.drawable.btn_sel);
                ten.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                foreverLight.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                break;
            case 2:
                two.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                five.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                ten.setTextColor(getResources().getColor(R.color.common_white));
                foreverLight
                        .setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                two.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                five.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                ten.setBackgroundResource(R.drawable.btn_sel);
                foreverLight.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                break;
            case 3:
                two.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                five.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                ten.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
                foreverLight.setTextColor(getResources().getColor(R.color.common_white));
                two.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                five.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                ten.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
                foreverLight.setBackgroundResource(R.drawable.btn_sel);
                break;
        // case 4:
        // simulation.setTextColor(getResources().getColor(R.color.common_white));
        // leftORrightMove.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
        // simulation.setBackgroundResource(R.drawable.btn_sel);
        // leftORrightMove.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
        // break;
        // case 5:
        // simulation.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
        // leftORrightMove.setTextColor(getResources().getColor(R.color.common_white));
        // simulation.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
        // leftORrightMove.setBackgroundResource(R.drawable.btn_sel);
        // break;
        }
    }

    private void showTurnModeSelect(int selection) {
        mTurnModeReal.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
        mMoveLeftRight.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
        mScrollUpdown.setTextColor(getResources().getColor(R.color.menu_set_light_time_text));
        mTurnModeReal.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
        mMoveLeftRight.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
        mScrollUpdown.setBackgroundResource(R.drawable.icon_menu_more_set_btn);
        switch (selection) {
            case BookContentView.PAGETURN_MODE_SMOOTH:
                mMoveLeftRight.setTextColor(getResources().getColor(R.color.common_white));
                mMoveLeftRight.setBackgroundResource(R.drawable.btn_sel);
                break;
            case BookContentView.PAGETURN_MODE_BOOK:
                mTurnModeReal.setTextColor(getResources().getColor(R.color.common_white));
                mTurnModeReal.setBackgroundResource(R.drawable.btn_sel);
                break;
            case BookContentView.PAGETURN_MODE_SCROLL:
                mScrollUpdown.setTextColor(getResources().getColor(R.color.common_white));
                mScrollUpdown.setBackgroundResource(R.drawable.btn_sel);
                break;
            default:
                break;
        }
    }


    @Override
    public void activityLogicForward() {
        // TODO Auto-generated method stub

    }

    @Override
    public void activityInitData() {
        // TODO Auto-generated method stub

    }

    private void setScreenTime(int value) {
        SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentKeepScreenTime(value);
    }

    private void initScreenTime() {
        int result =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                        .getBookContentKeepScreenTime();
        int select = 1;
        switch (result) {
            case NEVER_SLEEP_TIME:
                select = 3;
                break;
            case 5 * 60 * 1000:
                select = 0;
                break;
            case 10 * 60 * 1000:
                select = 1;
                break;
            case SYSTEM_TIME:
                select = 2;
                break;
        }
        showSelect(select);
    }

    private void initTurnMode() {
        int mPageTurnMode =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentPageTurnMode();
        showTurnModeSelect(mPageTurnMode);
    }

}
